<!--
	o   Will allow Other Parties to visualize a list of their running ads
	o   Will display information about the ad like number of hits and how the ad costs so far.
	o   Should allow to delete ads and pay balance if unpaid.
-->

<h1>Advertisement List Page</h1>

<?php
	$USRID = $_SESSION["USRID"];
	if(isset($_POST['mode'])) $mode = $_POST['mode'];
	else $mode = 'view';
	if($mode == 'save')
	{
		save($_POST['prevmode']);
		$mode = 'view';
	}
	echo '<h2>'.$mode.'</h2>';
	if ( $mode == "add" )
		$sql = "SELECT * FROM ADVERTISEMENTS LIMIT 1;";
	else $sql = "SELECT * FROM `ADVERTISEMENTS` WHERE OPEID IN
			   (SELECT OPEID FROM OTHER_PARTIES WHERE USRID = $USRID);";
	include('connect.php');
	$result = mysql_query($sql);
	include('disconnect.php');
	if(!$result) die("<br />ERROR535 on:<br/><pre>$sql<pre><br />SQL Command Error: " . mysql_error());
	$swaps = array(	'ADTID' 	=>	'SKIP',
			'OPEID'		=>	'SKIP',
			'DISPLAYS'	=>	'<center>VALUEMARKER left</center>');
	if($mode == 'edit')
	{
		$swaps['ADTID'] = '<input type="hidden" name="KEYMARKER" value="VALUEMARKER"/>';
		$swaps['DEFAULT'] = '<input type="text" name="KEYMARKER" value="VALUEMARKER"/>';
	}
	elseif($mode == 'add')
	{
		$swaps['DEFAULT'] = '<input type="text" name="KEYMARKER"/>';
		$swaps['DISPLAYS'] = '<input type="text" name="KEYMARKER" value="5000"/>';
	}
	elseif($mode == 'view')
	{
		$swaps['LINE3_URL'] = '<a href="VALUEMARKER">VALUEMARKER<a/>';
	}

	echo '<form action="?page=advertisement-list" method="post">';
	printTable($result, $swaps);	

	if($mode == "view")
	{
		echo '
		<input type="hidden" name="mode" value="edit"/>
		<input type="submit" value="Edit"/>
		</form>
		<form action="?page=advertisement-list" method="post">
		<input type="hidden" name="mode" value="add"/>
		<input type="submit" value="Add"/>';
	}
	else
	{
		echo "
		<input type='hidden' name='prevmode' value='$mode'/>
		<input type='hidden' name='mode' value='save'/>
		<br />
		<input type='submit' value='Save'/>";
	}	
	echo '</form>';

function save($prevmode)
{
	if ($prevmode == 'edit')
	{
		$r = 0;
		while(isset($_POST["ADTID-$r"]))
		{
			$ADTID = $_POST["ADTID-$r"];
			$LINE1 = $_POST["LINE1-$r"];
			$LINE2 = $_POST["LINE2-$r"];
			$LINE3_URL = $_POST["LINE3_URL-$r"];
			++$r;
			$sql = "UPDATE ADVERTISEMENTS 
				SET LINE1 = '$LINE1', LINE2 = '$LINE2', LINE3_URL = '$LINE3_URL' 
				WHERE ADTID = $ADTID;";
			include('connect.php');
			$result = mysql_query($sql);
			include('disconnect.php');
			if(!$result) die("<br />ERROR535 on:<br/><pre>$sql<pre><br />SQL Command Error: " . mysql_error());
		}

	}
	elseif ($prevmode == 'add')
	{
		$LINE1 = $_POST["LINE1"];
		$LINE2 = $_POST["LINE2"];
		$LINE3_URL = $_POST["LINE3_URL"];
		$DISPLAYS = $_POST["DISPLAYS"];
		
		if(trim($LINE1) == '')
			echo "<p>Error: LINE1 is blank!<br/>Ad was not added.</p>";
		elseif(trim($LINE2) == '')
			echo "<p>Error: Error: LINE2 is blank!<br/>Ad was not added.</p>";
		elseif(trim($LINE3_URL) == '')
			echo "<p>Error: Error: LINE3_URL is blank!<br/>Ad was not added.</p>";
		elseif($DISPLAYS < 5000)
			echo "<p>Error: Displays $DISPLAYS < 5000!<br/>Ad was not added.</p>";
		else
		{
			$USRID = $_SESSION["USRID"];
			$sql = "INSERT INTO ADVERTISEMENTS(LINE1, LINE2, LINE3_URL, DISPLAYS, OPEID)
				VALUES('$LINE1', '$LINE2', '$LINE3_URL', '$DISPLAYS', 
					(SELECT OPEID FROM OTHER_PARTIES WHERE USRID = $USRID));";
			include('connect.php');
			$result = mysql_query($sql);
			include('disconnect.php');
			if(!$result) die("<br />ERROR535 on:<br/><pre>$sql<pre><br />SQL Command Error: " . mysql_error());
		}

	}

}
?>
